package com.sql;

import java.util.*;
import java.io.*;


public class PropertyManager {

    private static PropertyManager manager = null;

    private static Object managerLock = new Object();

	private static String file=null;

    private static Properties properties = null;

    private Object propertiesLock = new Object();

    public static String getProperty(String name) {
        if (manager == null) {
            synchronized(managerLock) {
                if (manager == null) {
                    manager = new PropertyManager();
					setFile();
                }
            }
        }
        return manager.getProp(name);
    }

	@SuppressWarnings("static-access")
	public static void reset(){
        if (manager == null) {
            synchronized(managerLock) {
                if (manager == null) {
                    manager = new PropertyManager();
					setFile();
                }
            }
        }
		manager.loadProps();
	}

	private static void setFile(){
		try{
		    file=manager.getClass().getResource(".").getFile() + "database.conf";
			return;
		}catch(Exception e){
			e.printStackTrace();
			file=null;
			return;
		}
	}

    public static Enumeration propertyNames() {
        if (manager == null) {
            synchronized(managerLock) {
                if (manager == null) {
                    manager = new PropertyManager();
					setFile();
                }
            }
        }
        return manager.propNames();
    }

    protected String getProp(String name) {
        if (properties == null) {
            synchronized(propertiesLock) {
                if (properties == null) {
                    loadProps();
                }
            }
        }
        String property = properties.getProperty(name);
        if (property == null) {
            return null;
        }
        else {
            return property.trim();
        }
    }

    protected Enumeration propNames() {

        if (properties == null) {
            synchronized(propertiesLock) {
                if (properties == null) {
                    loadProps();
                }
            }
        }
        return properties.propertyNames();
    }

    private static void loadProps() {
	   properties = new Properties();
	   InputStream in = null;
	   try{
	        in =new FileInputStream(file);
			properties.load(in);
        }
        catch (Exception e) {
            System.err.println("Error reading Jive properties in PropertyManager.loadProps() " + e);
			return;
        }
        finally {
            try {
                in.close();
            } 
			catch (Exception e) {
			   return;
			}
        }
    }
}